package com.lanhe.pckageManage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.utils.SecurityUtils;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.pckageManage.domain.LhPackageManage;
import com.lanhe.pckageManage.mapper.LhPackageManageMapper;
import com.lanhe.pckageManage.service.ILhPackageManageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 包管理(LhPackageManage)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-02 22:51:15
 */
@Service
public class LhPackageManageServiceImpl extends ServiceImpl<LhPackageManageMapper, LhPackageManage> implements ILhPackageManageService {

    @Resource
    LhPackageManageMapper packageManageMapper;

    /**
     * 分页查询所有数据
     *
     * @param packageManage 实体
     * @return 安装包集合信息
     */
    @Override
    public List<LhPackageManage> selectIdentifierBindList(LhPackageManage packageManage) {
        LambdaQueryWrapper<LhPackageManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhPackageManage::getDelFlag,Constants.GeneralIntType.ZERO.toString());
        queryWrapper.like(StringUtils.isNotBlank(packageManage.getName()),LhPackageManage::getName,packageManage.getName());
        queryWrapper.eq(StringUtils.isNotBlank(packageManage.getManufacturer()),LhPackageManage::getManufacturer,packageManage.getManufacturer());
        queryWrapper.eq(null != packageManage.getStatus(),LhPackageManage::getStatus,packageManage.getStatus());
        queryWrapper.eq(null != packageManage.getIsPopular(),LhPackageManage::getIsPopular,packageManage.getIsPopular());
        queryWrapper.ge(ObjectUtil.isNotNull(packageManage.getBeginTime()), LhPackageManage::getCreateTime, packageManage.getBeginTime());
        queryWrapper.le(ObjectUtil.isNotNull(packageManage.getEndTime()), LhPackageManage::getCreateTime, packageManage.getEndTime());
        queryWrapper.orderByDesc(LhPackageManage::getPackageId);
        return packageManageMapper.selectList(queryWrapper);
    }

    /**
     * 新增包信息
     *
     * @param packageManage 实体
     * @return 结果
     */
    @Override
    public int insertPackageManage(LhPackageManage packageManage) {
        packageManage.setStatus(Constants.GeneralIntType.ZERO.toString());
        packageManage.setDelFlag(Constants.GeneralIntType.ZERO.toString());
        packageManage.setCreateBy(SecurityUtils.getUsername());
        packageManage.setCreateTime(new Date());
        return packageManageMapper.insert(packageManage);
    }

    /**
     * 修改包信息
     *
     * @param packageManage 实体
     * @return 结果
     */
    @Override
    public int updatePackageManage(LhPackageManage packageManage) {

        LambdaUpdateWrapper<LhPackageManage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LhPackageManage::getPackageId,packageManage.getPackageId());
        packageManage.setUpdateBy(SecurityUtils.getUsername());
        packageManage.setUpdateTime(new Date());
        return packageManageMapper.update(packageManage,updateWrapper);
    }

    /**
     * 修改包信息
     *
     * @param packageId 包ID
     * @return 结果
     */
    @Override
    public LhPackageManage selectPackageManageById(Long packageId) {

        LambdaQueryWrapper<LhPackageManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhPackageManage::getPackageId,packageId);

        return packageManageMapper.selectOne(queryWrapper);
    }


    /**
     *  删除
     *
     * @param packageId 包ID
     * @return int
     * @author zhanggl
     * @date 2023/12/4
     */
    @Override
    public int deletePackageManageById(Long packageId) {

        LambdaUpdateWrapper<LhPackageManage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LhPackageManage::getPackageId,packageId)
                .set(LhPackageManage::getDelFlag,Constants.GeneralIntType.TWO.toString());

        return packageManageMapper.update(null,updateWrapper);
    }

    /**
     * 获取厂商
     *
     * @return java.util.List<com.lanhe.pckageManage.domain.LhPackageManage>
     * @author zhanggl
     * @date 2023/12/11
     */
    @Override
    public List<LhPackageManage> getManufacturer() {

        LambdaQueryWrapper<LhPackageManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhPackageManage::getDelFlag,Constants.GeneralIntType.ZERO.toString());
        queryWrapper.groupBy(LhPackageManage::getManufacturer);
        return packageManageMapper.selectList(queryWrapper);
    }

    /**
     * 更新下载次数
     *
     * @param packageId 包ID
     * @return int
     * @author zhanggl
     * @date 2023/12/11
     */
    @Override
    public int updateDownloadNumber(Long packageId) {
        LambdaQueryWrapper<LhPackageManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhPackageManage::getPackageId,packageId);
        LhPackageManage packageManage = packageManageMapper.selectOne(queryWrapper);

        LambdaUpdateWrapper<LhPackageManage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LhPackageManage::getPackageId,packageId)
                .set(LhPackageManage::getDownNumber, packageManage.getDownNumber()+1);

        return packageManageMapper.update(null,updateWrapper);
    }
}

